<?php
/**
 * Application Routing Mapping
 *
 * @package /
 * @author  Vu Tran <vutran@perselab.com>
 * @since   Version 1.0
 */

// Controllers
require_once "Controllers/services.php";
require_once "Controllers/imageController.php";

// CategoriesToXML service
$app->get('/categories', function() use ($app){
	$services = new Services();
	$services->CategoriesToXML($app);
});

$app->post('/categories', function() use ($app){
	$services = new Services();
	$services->CategoriesToXML($app);
});

//login form
$app->get('/loginf',function() use ($app){
	echo '<form name="input" action="'.CTX.'login" method="post">';
	echo 'Username: <input type="text" name="un" value="duymoc" />';
	echo 'Password: <input type="password" name="pw" value="password" />';
	echo '<input type="submit" value="Submit" />';
	echo '</form>';
});
//login
$app->post('/login',function() use ($app){
	require_once "Controllers/auth.php";
	$username = $app->request()->post('un');
	$password = $app->request()->post('pw');
	$auth = new Auth();
	$auth->login($app,$username,$password);
});

//register form
$app->get('/registerf',function() use ($app){
	echo '<form name="input" action="'.CTX.'register" method="post">';
	echo 'Username: <input type="text" name="un" value="duymoc" /><br/>';
	echo 'Password: <input type="password" name="pw" value="password" /><br/>';
	echo 'Username: <input type="text" name="em" value="duymoc@yahoo.com" /><br/>';
	echo '<input type="submit" value="Submit" />';
	echo '</form>';
});

//register
$app->post('/register',function() use ($app){
	require_once "Controllers/auth.php";
	$username = $app->request()->post('un');
	$password = $app->request()->post('pw');
	$email = $app->request()->post('em');
	$auth = new Auth();
	$auth->register($app,$username,$password,$email);
});

//recipes form
$app->get('/recipesf',function() use ($app){
	echo '<form name="input" action="'.CTX.'recipes" method="post">';
	echo 'User Id: <input type="text" name="uid" value="" /><br/>';
	echo 'Category Id: <input type="text" name="cid" value="" /><br/>';
	echo '<input type="submit" value="Submit" />';
	echo '</form>';
});

//recipes
$app->post('/recipes',function() use ($app){
	$userId = $app->request()->post('uid');
	$categoryId = $app->request()->post('cid');
	if($userId == null || $userId == false || $userId == 0 || $userId == '') $userId = false;
	if($categoryId == null || $categoryId == false || $categoryId == 0 || $categoryId == '' ) $categoryId = false;
	$services = new Services();
	$services->recipesToXml($app,$userId,$categoryId);
});

//profile form
$app->get('/profilef',function() use ($app){
	echo '<form name="input" action="'.CTX.'profile" method="post">';
	echo 'User id: <input type="text" name="uid" value="1" />';
	echo '<input type="submit" value="Submit" />';
	echo '</form>';
});
//profile
$app->post('/profile',function() use ($app){
	require_once "Controllers/auth.php";
	$userid = $app->request()->post('uid');
	$auth = new Auth();
	$auth->profile($app,$userid);
});

//recipe form
$app->get('/recipef',function() use ($app){
	echo '<form name="input" action="'.CTX.'recipe" method="post">';
	echo 'Recipe id: <input type="text" name="rid" value="1" />';
	echo '<input type="submit" value="Submit" />';
	echo '</form>';
});
//recipe
$app->post('/recipe',function() use ($app){
	$recipeId = $app->request()->post('rid');
	$services = new Services();
	$services->recipeDetailToXml($app,$recipeId);
});

//search form
$app->get('/searchf',function() use ($app){
	echo '<form name="input" action="'.CTX.'search" method="post">';
	echo 'Recipe name: <input type="text" name="key" value="" />';
	echo '<input type="submit" value="Search" />';
	echo '</form>';
});
//search
$app->post('/search',function() use ($app){
	$key = $app->request()->post('key');
	if($key == '' || $key == null || $key == false)
		$key = '';
	
	$services = new Services();
	$services->searchToXml($app,$key);
});

//add recipe form
$app->get('/recipe/addf',function() use ($app){
	
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/add" method="post">';
	echo 'Recipe Img: <input type="file" name="ri[]" id="file" /><br/>';
	echo 'Recipe Img: <input type="file" name="ri[]" id="file" /><br/>';
	echo 'Category Id: <input type="text" name="cid[]" value="1" /><br/>';
	echo 'Category Id: <input type="text" name="cid[]" value="2" /><br/>';
	echo 'User Id: <input type="text" name="uid" value="1" /><br/>';
	echo 'Recipe Name: <input type="text" name="rn" value="" /><br/>';
	echo 'Recipe Serving: <input type="text" name="rs" value="5" /><br/>';
	echo '<input type="submit" value="Them" />';
	echo '</form>';
});
//add recipe
$app->post('/recipe/add',function() use ($app){
	$user_id = $app->request()->post('uid');
	$recipe_name = $app->request()->post('rn');
	$recipe_catg = $app->request()->post('cid');
	$recipe_serving = $app->request()->post('rs');
	
	$files = array();
	if( isset($_FILES['ri']) && count($_FILES['ri']) > 0 ){
	    foreach ($_FILES['ri'] as $k => $l) {
	        foreach ($l as $i => $v) {
	            if (!array_key_exists($i, $files))
	                $files[$i] = array();
	            $files[$i][$k] = $v;
	        }
	    }
	}
	
	$recipe = array(
		'user_id' => $user_id,
		'name' => $recipe_name,
		'serving' => $recipe_serving,
		'category' => $recipe_catg,
		'images' => $files
	);

	$services = new Services();
	$services->addRecipe($app,$recipe);
});

//delete recipe form
$app->get('/recipe/deletef',function() use ($app){
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/delete" method="post">';
	echo 'User Id: <input type="text" name="uid" value="1" /><br/>';
	echo 'Recipe id: <input type="text" name="rid" value="" /><br/>';
	echo '<input type="submit" value="Delete" />';
	echo '</form>';
});
//add recipe
$app->post('/recipe/delete',function() use ($app){
	$user_id = $app->request()->post('uid');
	$recipe_id = $app->request()->post('rid');
	
	$recipe = array(
		'user_id' => $user_id,
		'recipe_id' => $recipe_id
	);

	$services = new Services();
	$services->deleteRecipe($app,$recipe);
});

//update recipe form
$app->get('/recipe/updatef',function() use ($app){
	
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/update" method="post">';
	//echo 'Recipe Img: <input type="file" name="ri[]" id="file" /><br/>';
	//echo 'Recipe Img: <input type="file" name="ri[]" id="file" /><br/>';
	echo 'Recipe Id: <input type="text" name="rid" value="1" /><br/>';
	echo 'Category Id: <input type="text" name="cid[]" value="1" /><br/>';
	echo 'Category Id: <input type="text" name="cid[]" value="2" /><br/>';
	echo 'User Id: <input type="text" name="uid" value="1" /><br/>';
	echo 'Recipe Name: <input type="text" name="rn" value="" /><br/>';
	echo 'Recipe Serving: <input type="text" name="rs" value="5" /><br/>';
	echo '<input type="submit" value="Them" />';
	echo '</form>';
});

//update recipe
$app->post('/recipe/update',function() use ($app){
	$recipe_id = $app->request()->post('rid');
	$recipe_name = $app->request()->post('rn');
	$recipe_catg = $app->request()->post('cid');
	$recipe_serving = $app->request()->post('rs');
	
	$files = array();
	if( isset($_FILES['ri']) && count($_FILES['ri']) > 0 ){
	    foreach ($_FILES['ri'] as $k => $l) {
	        foreach ($l as $i => $v) {
	            if (!array_key_exists($i, $files))
	                $files[$i] = array();
	            $files[$i][$k] = $v;
	        }
	    }
	}
	
	$recipe = array(
		'recipe_id' => $recipe_id,
		'name' => $recipe_name,
		'serving' => $recipe_serving,
		'category' => $recipe_catg,
		'images' => $files
	);

	$services = new Services();
	$services->updateRecipe($app,$recipe);
});

//add ingredient form
$app->get('/recipe/ingredient/addf',function() use ($app){
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/ingredient/add" method="post">';
	echo '-----------Ingredients-------------------<br/>';
	echo 'Recipe id: <input type="text" name="rid" value="1" /><br/>';
	echo 'Ingredient Img: <input type="file" name="ii" /><br/>';
	echo 'Ingredient Name: <input type="text" name="in" value="" /><br/>';
	echo 'Ingredient Desc: <input type="text" name="idesc" value="" /><br/>';
	echo 'Quantity: <input type="text" name="iqty" value="1/2" /><br/>';
	echo 'unit: <input type="text" name="iunit" value=cup"" /><br/>';
	echo '<input type="submit" value="Them" />';
	echo '</form>';
});

//add recipe
$app->post('/recipe/ingredient/add',function() use ($app){
	
	$ingredient = array(
		'recipe_id' => $app->request()->post('rid'),
		'name' => $app->request()->post('in'),
		'description' => $app->request()->post('idesc'),
		'quantity' => $app->request()->post('iqty'),
		'unit' => $app->request()->post('iunit'),
		'images' => (isset($_FILES['ii'])) ? $_FILES['ii'] : null
	);

	$services = new Services();
	$services->addIngredient($app,$ingredient);
});

//delete ingredient form
$app->get('/recipe/ingredient/deletef',function() use ($app){
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/ingredient/delete" method="post">';
	echo 'User Id: <input type="text" name="uid" value="1" /><br/>';
	echo 'Recipe id: <input type="text" name="rid" value="" /><br/>';
	echo 'Ingredient id: <input type="text" name="iid" value="" /><br/>';
	echo '<input type="submit" value="Delete" />';
	echo '</form>';
});
//add recipe
$app->post('/recipe/ingredient/delete',function() use ($app){
	$user_id = $app->request()->post('uid');
	$recipe_id = $app->request()->post('rid');
	$ingredient_id = $app->request()->post('iid');
	
	$recipe = array(
		'user_id' => $user_id,
		'recipe_id' => $recipe_id,
		'ingredient_id' => $ingredient_id
	);

	$services = new Services();
	$services->deleteRecipeIngredient($app,$recipe);
});

//add step form
$app->get('/recipe/step/addf',function() use ($app){
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/step/add" method="post">';
	echo '-----------Steps-------------------<br/>';
	echo 'Recipe id: <input type="text" name="rid" value="1" /><br/>';
	echo 'Step Name: <input type="text" name="sname" value="" /><br/>';
	echo 'Step Desc: <input type="text" name="sdesc" value="" /><br/>';
	echo '<input type="submit" value="Them" />';
	echo '</form>';
});

$app->post('/recipe/step/add',function() use ($app){

	$steps = array(
		'recipe_id' => $app->request()->post('rid'),
		'name' => $app->request()->post('sname'),
		'description' => $app->request()->post('sdesc'),
		'note' => '',
		//'images' => $_FILES['si']
	);

	$services = new Services();
	$services->addStep($app,$steps);
});

//delete step form
$app->get('/recipe/step/deletef',function() use ($app){
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'recipe/step/delete" method="post">';
	echo 'User Id: <input type="text" name="uid" value="1" /><br/>';
	echo 'Recipe id: <input type="text" name="rid" value="" /><br/>';
	echo 'Step id: <input type="text" name="sid" value="" /><br/>';
	echo '<input type="submit" value="Delete" />';
	echo '</form>';
});
//add recipe
$app->post('/recipe/step/delete',function() use ($app){
	$user_id = $app->request()->post('uid');
	$recipe_id = $app->request()->post('rid');
	$step_id = $app->request()->post('sid');
	
	$recipe = array(
		'user_id' => $user_id,
		'recipe_id' => $recipe_id,
		'step_id' => $step_id
	);

	$services = new Services();
	$services->deleteRecipeStep($app,$recipe);
});

//upload image form
$app->get('/image/uploadf',function() use ($app){
	$image = Model::factory('Image')->create();
	//$image->upload();
	
	echo '<form enctype="multipart/form-data" name="input" action="'.CTX.'image/upload" method="post">';
	echo 'Recipe Img: <input type="file" name="file" id="file" /><br/>';
	echo '<input type="submit" value="Upload" />';
	echo '</form>';
});

//upload image
$app->post('/image/upload',function() use ($app){
	require_once "Controllers/imageUploadController.php";
	
	$upload = new imageUpload();
	$image = $upload->upload($_FILES['file']);
	
	if( $image ){
		echo $image->id;
		echo '<br/>';
		echo $image->name;
		echo '<br/>';
		echo $image->path;
		echo '<br/>';
		echo '<img src="'.CTX.'image/'.$image->id.'" alt="hinh">';
	}else{
		echo 'co loi xay ra';
	}
});
// Blog Homepage.
$app->get('/', function() use ($app) {
	$recipes = Model::factory('Recipe')
					//->order_by_desc('timestamp')
					->find_many();
					
	return $app->render('blog_home.html', array('recipes' => $recipes));	
});

// Blog View.
$app->get('/view/(:id)', function($id) use ($app) {
	$article = Model::factory('Article')->find_one($id);
	if (! $article instanceof Article) {
		$app->notFound();
	}
	
	return $app->render('blog_detail.html', array('article' => $article));
});



// Admin Home.
$app->get('/admin', $authCheck, function() use ($app) {
	$articles = Model::factory('Recipe')
					//->order_by_desc('timestamp')
					->find_many();
					
	return $app->render('admin_home.html', array('articles' => $articles));
});

// Admin Add.
$app->get('/admin/add', $authCheck, function() use ($app) {
	return $app->render('admin_input.html', array('action_name' => 'Add', 'action_url' => '/admin/add'));
});	

// Admin Add - POST.
$app->post('/admin/add', $authCheck, function() use ($app) {
	$article 			= Model::factory('Article')->create();
	$article->title 	= $app->request()->post('title');
	$article->author 	= $app->request()->post('author');
	$article->summary 	= $app->request()->post('summary');
	$article->content 	= $app->request()->post('content');
	$article->timestamp = date('Y-m-d H:i:s');
	$article->save();
	
	$app->redirect('/admin');
});

// Admin Edit.
$app->get('/admin/edit/(:id)', $authCheck, function($id) use ($app) {
	$article = Model::factory('Article')->find_one($id);
	if (! $article instanceof Article) {
		$app->notFound();
	}	
	
	return $app->render('admin_input.html', array(
		'action_name' 	=> 	'Edit', 
		'action_url' 	=> 	'/admin/edit/' . $id,
		'article'		=> 	$article
	));
});

// Admin Edit - POST.
$app->post('/admin/edit/(:id)', $authCheck, function($id) use ($app) {
	$article = Model::factory('Article')->find_one($id);
	if (! $article instanceof Article) {
		$app->notFound();
	}
	
	$article->title 	= $app->request()->post('title');
	$article->author 	= $app->request()->post('author');
	$article->summary 	= $app->request()->post('summary');
	$article->content 	= $app->request()->post('content');
	$article->timestamp = date('Y-m-d H:i:s');
	$article->save();
	
	$app->redirect('/admin');
});

// Admin Delete.
$app->get('/admin/delete/(:id)', $authCheck, function($id) use ($app) {
	$article = Model::factory('Article')->find_one($id);
	if ($article instanceof Article) {
		$article->delete();
	}
	
	$app->redirect('/admin');
});
$app->get('/image(/:id(/:width(/:height)))', function ($id = 0, $width = 0, $height = 0) use ($app) {
   	$imageController = new ImageController();
	$imageController->loadImage($app, $id, $width, $height); 
});